package com.century.sjt.window;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.AddBankActivity;
import com.century.sjt.adapter.ChoosePayAdapter;
import com.century.sjt.entity.BankCard;
import com.century.sjt.entity.Merchant;

import java.util.List;

/**
 * Created by 炳基 on 2016/7/26.
 */
public class ChoosePayPopupWindow extends PopupWindow{
    private Context mContext;
    private Button btn_ok;
    private View mMenuView;
    private RequestQueue mQueue;
    //翻译监听
    private PayChooseWindowListItemClickListener  listener;
    private BankCard payBankCard;
    public CheckBox checkBox;
    private ListView listView;
    private ChoosePayAdapter adapter;
    private Handler mHandler;
    private LinearLayout liftLayout;
    private LinearLayout layout_addBanckpay;
    private View view_add;
    private TextView tvTitle;
    private List<BankCard> cardList;
    private String mType;
    private TextView left_icon;
    private String toType;//支付入口

    public ChoosePayPopupWindow(final Context context, final List<BankCard> mlist, final String type, final Merchant Shop,final String orderId,final String toType) {
        super(context);
        this.mContext = context;
        this.cardList = mlist;
        this.mType = type;
        this.toType = toType;//1：商家 2：订单 3：充值

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.sjt_choosepay_window_item, null);
        mQueue = Volley.newRequestQueue(mMenuView.getContext());
//        checkBox = (CheckBox) mMenuView.findViewById(R.id.item_check);
//        checkBox.setChecked(true);
        listView = (ListView) mMenuView.findViewById(R.id.lv_listview);
        liftLayout = (LinearLayout) mMenuView.findViewById(R.id.choose_pay_liftbtn);
        layout_addBanckpay = (LinearLayout) mMenuView.findViewById(R.id.layout_addbankpay) ;
        view_add = (View) mMenuView.findViewById(R.id.view_add);
        tvTitle = (TextView) mMenuView.findViewById(R.id.window_pay_title);
        left_icon = (TextView) mMenuView.findViewById(R.id.left_icon);
        Typeface iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
        left_icon.setTypeface(iconfont);
        //设置按钮监听

        //设置PayPopupWindow的View
        this.setContentView(mMenuView);
        //设置PayPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置PayPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PayPopupWindow弹出窗体可点击
        this.setFocusable(true);

        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置PayPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        if ("5".equals(type)){
            //设置PayPopupWindow弹出窗体动画效果
            this.setAnimationStyle(R.style.AnimBottom);
            layout_addBanckpay.setVisibility(View.VISIBLE);
            view_add.setVisibility(View.VISIBLE);
            tvTitle.setText(mContext.getResources().getString(R.string.text_choose_pay_card));

        }else {
            //设置PayPopupWindow弹出窗体动画效果
            this.setAnimationStyle(R.style.AnimRight);
        }
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        };

        adapter = new ChoosePayAdapter(mContext, cardList, mHandler,"0");//0置灰，1显示信用卡


        listView.setAdapter(adapter);
        // 设置适配器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if ("1".equals(mlist.get(i).getAvailable())){
                    String name = mlist.get(i).getCardName();
                    listener.onItemClick(mlist.get(i));
                    listener.onPayTypeClick(mType);
                    dismiss();
                }else if ("1".equals(mlist.get(i).getIsActive())){
                    final String phone = mlist.get(i).getPhone();
                    final String bankCardNo = mlist.get(i).getBankCardNo();
                    final String bankCode = mlist.get(i).getBankCode();
                    final String bankName = mlist.get(i).getCardName();
                    final String bankType = mlist.get(i).getCardTypeName();
//                    final String status =  mlist.get(i).getStatus();
                    final AlertDialog alertDialog = new AlertDialog.Builder(context)
                            .setCancelable(true)
                            .show();
                    Window window = alertDialog.getWindow();
                    window.setContentView(R.layout.sjt_getmonilecode_alertdialog);
                    TextView tv_message = (TextView) window.findViewById(R.id.tv_dialog_message);
                    tv_message.setText("立即激活快捷支付？") ;
                    Button btn_no = (Button) window.findViewById(R.id.btn_dialog_no);
                    btn_no.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alertDialog.dismiss();
                        }
                    });
                    Button btn_ye = (Button) window.findViewById(R.id.btn_dialog_ye);
                    btn_ye.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alertDialog.dismiss();
                            listener.onDismissClick();
                            dismiss();
                            Intent intent = new Intent();
                            intent.setClass(context, AddBankActivity.class);
                            Bundle bundle = new Bundle();
                            if ("1".equals(toType)){
                                bundle.putSerializable("Merchant",Shop);
                                intent.putExtra("type","4");

                            }else if ("2".equals(toType)){
                                intent.putExtra("type","8");
                                intent.putExtra("orderId",orderId);
                            }else if ("3".equals(toType)){
                                intent.putExtra("type","1");
                            }

                            bundle.putString("phone",phone);
                            bundle.putString("bankCardNo",bankCardNo);
                            bundle.putString("bankCode", bankCode);
                            bundle.putString("bankName", bankName);
                            bundle.putString("bankType",bankType);
                            bundle.putString("status","0");
                            intent.putExtras(bundle);

                            context.startActivity(intent);

                        }
                    });
                }

            }
        });
        liftLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        layout_addBanckpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("type", type);
                intent.putExtras(bundle);
                intent.setClass(mContext, AddBankActivity.class);
                mContext.startActivity(intent);
            }
        });
    }
    public void notifyDataSetChanged(){
        Message msg = new Message();
        msg.what = 1;
        mHandler.sendMessage(msg);
    }
    public void setChoosePayWindowListItemClickListener(PayChooseWindowListItemClickListener listener) {
        this.listener = listener;
    }
    public interface PayChooseWindowListItemClickListener{
        /**
         * 支付选择事件监听
         * @param message
         * @return
         */
        void onItemClick(BankCard message);
        void onPayTypeClick(String Type);
        void onDismissClick();
    }
}
